function isString(data: any): data is string {
  return typeof data === 'string'
}

interface DbConConf {
  host: string
  port: number
  user: string
  password: string
  database: string
}

interface EnvConf {
  dev: DbConConf
  prod: DbConConf
}

class Conf {
  static conf: Conf = new Conf()
  env!: keyof EnvConf
  envConf!: EnvConf
  constructor() {
    this.env = process.env.NODE_ENV === 'dev' ? 'dev' : 'prod'
    this.initConf()
  }

  initConf() {
    this.envConf = {
      dev: {
        host: '127.0.0.1',
        port: 3306,
        user: 'admin',
        password: '123',
        database: 'dangdang'
      },
      prod: {
        host: '127.0.0.1',
        port: 3306,
        user: 'admin',
        password: '123',
        database: 'dangdang'
      }
    }
  }

  getConf(): DbConConf
  getConf(key: string): string | number
  getConf(key: string | null = null): any {
    if (this.isDbConConfKey(key)) return this.envConf[this.env][key]
    else return this.envConf[this.env]
  }
  isDbConConfKey(key: string | null): key is keyof DbConConf {
    if (isString(key) && key.length > 0) return key in this.envConf[this.env]
    else return false
  }
}

export default Conf.conf
